home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-03 / qbasicpg.zip / WHEREIS.BAS < prev    next >
BASIC Source File  |  1987-09-22  |  5KB  |  159 lines

  1. DEFINT A-Z
  2.  
  3. ' Declare symbolic constants used in program:
  4. CONST EOFTYPE = 0, FILETYPE = 1, DIRTYPE = 2, ROOT = "TWH"
  5.  
  6. DECLARE SUB ScanDir (PathSpec$, Level, FileSpec$, Row)
  7.  
  8. DECLARE FUNCTION MakeFileName$ (Num)
  9. DECLARE FUNCTION GetEntry$ (FileNum, EntryType)
  10.  
  11. CLS
  12. INPUT "File to look for"; FileSpec$
  13. PRINT
  14. PRINT "Enter the directory where the search should start"
  15. PRINT "(optional drive + directories). Press <ENTER> to begin"
  16. PRINT "the search in the root directory of the current drive."
  17. PRINT
  18. INPUT "Starting directory"; PathSpec$
  19. CLS
  20.  
  21. RightCh$ = RIGHT$(PathSpec$, 1)
  22.  
  23. IF PathSpec$ = "" OR RightCh$ = ":" OR RightCh$ <> "\" THEN
  24.    PathSpec$ = PathSpec$ + "\"
  25. END IF
  26.  
  27. FileSpec$ = UCASE$(FileSpec$)
  28. PathSpec$ = UCASE$(PathSpec$)
  29. Level = 1
  30. Row = 3
  31.  
  32. ' Make the top level call (level 1) to begin the search:
  33. ScanDir PathSpec$, Level, FileSpec$, Row
  34.  
  35. KILL ROOT + ".*"        ' Delete all temporary files created
  36.                         ' by the program.
  37.  
  38. LOCATE Row + 1, 1: PRINT "Search complete."
  39. END
  40. '
  41. ' ======================= GETENTRY ==========================
  42. '    This procedure processes entry lines in a DIR listing
  43. '    saved to a file.
  44. ' ===========================================================
  45. '
  46. FUNCTION GetEntry$ (FileNum, EntryType) STATIC
  47.  
  48.    ' Loop until a valid entry or end-of-file (EOF) is read:
  49.    DO UNTIL EOF(FileNum)
  50.       LINE INPUT #FileNum, EntryLine$
  51.       IF EntryLine$ <> "" THEN
  52.  
  53.          ' Get first character from the line for test:
  54.          TestCh$ = LEFT$(EntryLine$, 1)
  55.          IF TestCh$ <> " " AND TestCh$ <> "." THEN EXIT DO
  56.       END IF
  57.    LOOP
  58.  
  59.    ' Entry or EOF found, decide which:
  60.    IF EOF(FileNum) THEN
  61.       EntryType = EOFTYPE
  62.       GetEntry$ = ""
  63.  
  64.    ELSE           ' Not EOF, either a file or a directory.
  65.  
  66.       ' Build and return the entry name:
  67.       EntryName$ = RTRIM$(LEFT$(EntryLine$, 8))
  68.  
  69.       ' Test for extension and add to name if there is one:
  70.       EntryExt$ = RTRIM$(MID$(EntryLine$, 10, 3))
  71.       IF EntryExt$ <> "" THEN
  72.          GetEntry$ = EntryName$ + "." + EntryExt$
  73.       ELSE
  74.          GetEntry$ = EntryName$
  75.       END IF
  76.  
  77.       ' Determine the entry type, and return that
  78.       ' value to the point where GetEntry$ was called:
  79.       IF MID$(EntryLine$, 15, 3) = "DIR" THEN
  80.          EntryType = DIRTYPE            ' Directory
  81.       ELSE
  82.          EntryType = FILETYPE           ' File
  83.       END IF
  84.  
  85.    END IF
  86.  
  87. END FUNCTION
  88. '
  89. ' ===================== MAKEFILENAME$ =======================
  90. '    This procedure makes a file name from a root string
  91. '    ("TWH" - defined as a symbolic constant at the module
  92. '    level) and a number passed to it as an argument (Num).
  93. ' ===========================================================
  94. '
  95. FUNCTION MakeFileName$ (Num) STATIC
  96.  
  97.    MakeFileName$ = ROOT + "." + LTRIM$(STR$(Num))
  98.  
  99. END FUNCTION
  100. '
  101. ' ======================= SCANDIR ===========================
  102. '   This procedure recursively scans a directory for the
  103. '   file name entered by the user.
  104. '
  105. '   NOTE: The SUB header doesn't use the STATIC keyword
  106. '         since this procedure needs a new set of variables
  107. '         each time it is invoked.
  108. ' ===========================================================
  109. '
  110. SUB ScanDir (PathSpec$, Level, FileSpec$, Row)
  111.  
  112.    LOCATE 1, 1: PRINT "Now searching"; SPACE$(50);
  113.    LOCATE 1, 15: PRINT PathSpec$;
  114.  
  115.    ' Make a file specification for the temporary file:
  116.    TempSpec$ = MakeFileName$(Level)
  117.  
  118.    ' Get a directory listing of the current directory, and
  119.    ' save it in the temporary file:
  120.    SHELL "DIR " + PathSpec$ + " > " + TempSpec$
  121.  
  122.    ' Get the next available file number:
  123.    FileNum = FREEFILE
  124.  
  125.    ' Open the DIR listing file and scan it:
  126.    OPEN TempSpec$ FOR INPUT AS #FileNum
  127.  
  128.    ' Process the file, one line at a time:
  129.    DO
  130.  
  131.       ' Get an entry from the DIR listing:
  132.       DirEntry$ = GetEntry$(FileNum, EntryType)
  133.  
  134.       ' If entry is a file:
  135.       IF EntryType = FILETYPE THEN
  136.  
  137.          ' If the FileSpec$ string matches, print entry and
  138.          ' exit this loop:
  139.          IF DirEntry$ = FileSpec$ THEN
  140.             LOCATE Row, 1: PRINT PathSpec$; DirEntry$;
  141.             Row = Row + 1
  142.             EntryType = EOFTYPE
  143.          END IF
  144.  
  145.       ' If the entry is a directory, then make a recursive
  146.       ' call to ScanDir with the new directory:
  147.       ELSEIF EntryType = DIRTYPE THEN
  148.          NewPath$ = PathSpec$ + DirEntry$ + "\"
  149.          ScanDir NewPath$, Level + 1, FileSpec$, Row
  150.          LOCATE 1, 1: PRINT "Now searching"; SPACE$(50);
  151.          LOCATE 1, 15: PRINT PathSpec$;
  152.       END IF
  153.  
  154.    LOOP UNTIL EntryType = EOFTYPE
  155.  
  156.    ' Scan on this DIR listing file is finished, so close it:
  157.    CLOSE FileNum
  158. END SUB
  159.